﻿Imports System.Data.OleDb
Public Class frmSettings

#Region "NotifyIcon"

    Private Sub frmSettings_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        Try
            If Me.WindowState = FormWindowState.Minimized Then
                Me.Visible = False
                NotifyIcon.Visible = True
                NotifyIcon.ShowBalloonTip(1, "SkyCoffee", "SkyCoffee is Still Running!", ToolTipIcon.Info)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub NotifyIcon_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon.MouseDoubleClick
        Try
            Me.Visible = True
            Me.WindowState = FormWindowState.Normal
            NotifyIcon.Visible = False
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub mnuLaunchApplication_Click(sender As Object, e As EventArgs) Handles mnuLaunchApplication.Click
        Try
            Me.Visible = True
            Me.WindowState = FormWindowState.Normal
            NotifyIcon.Visible = False
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub mnuExitApplication_Click(sender As Object, e As EventArgs) Handles mnuExitApplication.Click
        Application.Exit()
    End Sub
#End Region

    Dim dsEmail As New DataSet()
    Dim intCurrentIndexEmail As Integer = 0
    Dim daEmail As New OleDbDataAdapter()
    Dim connEmail As New OleDbConnection()

    Dim strEmailID As String = 1
    Public Sub FillFields()
        strEmailID = dsEmail.Tables(0).Rows(intCurrentIndexEmail).Item("fldEmailID").ToString()
        txtServer.Text = dsEmail.Tables(0).Rows(intCurrentIndexEmail).Item("fldEmailServer").ToString()
        chkRequiresAuthentication.Checked = dsEmail.Tables(0).Rows(intCurrentIndexEmail).Item("fldEmailRequiresAuthentication").ToString()
        txtUsername.Text = dsEmail.Tables(0).Rows(intCurrentIndexEmail).Item("fldEmailUsername").ToString()
        txtPassword.Text = dsEmail.Tables(0).Rows(intCurrentIndexEmail).Item("fldEmailPassword").ToString()
        chkSSLConnection.Checked = dsEmail.Tables(0).Rows(intCurrentIndexEmail).Item("fldEmailSSLConnection").ToString()
        cmbProtocol.Text = dsEmail.Tables(0).Rows(intCurrentIndexEmail).Item("fldEmailProtocol").ToString()
    End Sub
    Private Sub frmSettings_Load(sender As Object, e As EventArgs) Handles Me.Load
        'Loads frmMenu
        pnlIcons.Controls.Clear()
        pnlIcons.Controls.Add(frmMenu.pnlIcons)

        _ChangeAuthStatus()
        _InitProtocols()

        'Load ToolTips
        AdminToolTips()

        'Retrieve from database tblCandidatesRetrieveDataForSMS
        connEmail.ConnectionString = "Provider=Microsoft.jet.oledb.4.0;" & _
                "Data Source=|DataDirectory|\SkyCoffee.mdb"

        daEmail.SelectCommand = New OleDbCommand("SELECT * FROM tblEmailSettings")
        daEmail.SelectCommand.Connection = connEmail

        daEmail.UpdateCommand = New OleDbCommand("UPDATE tblEmailSettings SET fldEmailServer = @fldEmailServer, fldEmailRequiresAuthentication = @fldEmailRequiresAuthentication, fldEmailUsername = @fldEmailUsername, fldEmailPassword = @fldEmailPassword, fldEmailSSLConnection = @fldEmailSSLConnection, fldEmailProtocol = @fldEmailProtocol WHERE fldEmailID = @fldEmailID")
        daEmail.UpdateCommand.Connection = connEmail
        daEmail.UpdateCommand.Parameters.Add("@fldEmailServer", OleDbType.VarChar, 40, "fldEmailServer")
        daEmail.UpdateCommand.Parameters.Add("@fldEmailRequiresAuthentication", OleDbType.Boolean, 40, "fldEmailRequiresAuthentication")
        daEmail.UpdateCommand.Parameters.Add("@fldEmailUsername", OleDbType.VarChar, 40, "fldEmailUsername")
        daEmail.UpdateCommand.Parameters.Add("@fldEmailPassword", OleDbType.VarChar, 40, "fldEmailPassword")
        daEmail.UpdateCommand.Parameters.Add("@fldEmailSSLConnection", OleDbType.Boolean, 40, "fldEmailSSLConnection")
        daEmail.UpdateCommand.Parameters.Add("@fldEmailProtocol", OleDbType.VarChar, 40, "fldEmailProtocol")

        daEmail.UpdateCommand.Parameters.Add("@fldEmailID", OleDbType.Numeric, 6, "fldEmailID")

        daEmail.InsertCommand = New OleDbCommand("INSERT INTO tblEmailSettings(fldEmailID, fldEmailServer, fldEmailRequiresAuthentication, fldEmailUsername, fldEmailPassword, fldEmailSSLConnection, fldEmailProtocol) VALUES(@fldEmailID,@fldEmailServer,@fldEmailRequiresAuthentication,@fldEmailUsername,@fldEmailPassword,@fldEmailSSLConnection,@fldEmailProtocol)")
        daEmail.InsertCommand.Connection = connEmail
        daEmail.InsertCommand.Parameters.Add("@fldEmailServer", OleDbType.VarChar, 40, "fldEmailServer")
        daEmail.InsertCommand.Parameters.Add("@fldEmailRequiresAuthentication", OleDbType.Boolean, 40, "fldEmailRequiresAuthentication")
        daEmail.InsertCommand.Parameters.Add("@fldEmailUsername", OleDbType.VarChar, 40, "fldEmailUsername")
        daEmail.InsertCommand.Parameters.Add("@fldEmailPassword", OleDbType.VarChar, 40, "fldEmailPassword")
        daEmail.InsertCommand.Parameters.Add("@fldEmailSSLConnection", OleDbType.Boolean, 40, "fldEmailSSLConnection")
        daEmail.InsertCommand.Parameters.Add("@fldEmailProtocol", OleDbType.VarChar, 40, "fldEmailProtocol")

        daEmail.InsertCommand.Parameters.Add("@fldEmailID", OleDbType.Numeric, 6, "fldEmailID")

        daEmail.DeleteCommand = New OleDbCommand("DELETE FROM tblEmailSettings WHERE fldEmailID = @fldEmailID")
        daEmail.DeleteCommand.Connection = connEmail
        daEmail.DeleteCommand.Parameters.Add("@fldEmailID", OleDbType.Numeric, 6, "fldEmailID")

        daEmail.Fill(dsEmail)

        If dsEmail.Tables(0).Rows.Count > 0 Then 'Check to see if the table is empty
            FillFields()
        Else
            MsgBox("No records exist in database")
        End If

    End Sub
    Public Sub AddNewRecord()
        'Reset form fields
        txtServer.Text = ""
        chkRequiresAuthentication.Checked = False
        txtUsername.Text = ""
        txtPassword.Text = ""
        chkSSLConnection.Checked = False
        cmbProtocol.Text = ""

        txtServer.Focus()
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        If txtServer.Text = "" Or txtUsername.Text = "" Or txtPassword.Text = "" Or cmbProtocol.Text = "" Then
            MsgBox("Please fill in all of the fields")
        Else

            If dsEmail.Tables(0).Rows.Count < 1 Then

                'Database Operations: Add all data to Access Database
                Dim drEmail As DataRow

                drEmail = dsEmail.Tables(0).NewRow()  'Gets a reference to a new row.
                drEmail("fldEmailID") = strEmailID

                drEmail("fldEmailServer") = txtServer.Text
                drEmail("fldEmailRequiresAuthentication") = chkRequiresAuthentication.CheckState
                drEmail("fldEmailUsername") = txtUsername.Text
                drEmail("fldEmailPassword") = txtPassword.Text
                drEmail("fldEmailSSLConnection") = chkSSLConnection.CheckState
                drEmail("fldEmailProtocol") = cmbProtocol.Text

                dsEmail.Tables(0).Rows.Add(drEmail)
                daEmail.Update(dsEmail)
                dsEmail.AcceptChanges()

                MsgBox("Record saved successfully")

            ElseIf dsEmail.Tables(0).Rows.Count > 0 Then

                'Database Operations: Add all data to Access Database
                Dim drEmail As DataRow

                drEmail = dsEmail.Tables(0).Rows(intCurrentIndexEmail) 'This gets a reference to the row currently being edited
                drEmail.BeginEdit()
                drEmail("fldEmailID") = strEmailID

                drEmail("fldEmailServer") = txtServer.Text
                drEmail("fldEmailRequiresAuthentication") = chkRequiresAuthentication.CheckState
                drEmail("fldEmailUsername") = txtUsername.Text
                drEmail("fldEmailPassword") = txtPassword.Text
                drEmail("fldEmailSSLConnection") = chkSSLConnection.CheckState
                drEmail("fldEmailProtocol") = cmbProtocol.Text
                drEmail.EndEdit()

                daEmail.Update(dsEmail)  'Ask the dataadapter to call the UpdateCommand and update the database
                dsEmail.AcceptChanges() 'Commits the change to the dataset.

                MsgBox("Record saved successfully")

            End If

        End If
    End Sub

    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        AddNewRecord()
    End Sub
    Public Sub _ChangeAuthStatus()
        txtUsername.Enabled = chkRequiresAuthentication.Checked
        txtPassword.Enabled = chkRequiresAuthentication.Checked
    End Sub
    Public Sub _InitProtocols()
        cmbProtocol.Items.Add("SMTP Protocol - Recommended")
        cmbProtocol.Items.Add("Exchange Web Service - 2007/2010")
        cmbProtocol.Items.Add("Exchange WebDav - 2000/2003")
        cmbProtocol.SelectedIndex = 0
    End Sub

    Private Sub chkRequiresAuthentication_CheckedChanged(sender As Object, e As EventArgs) Handles chkRequiresAuthentication.CheckedChanged
        _ChangeAuthStatus()
    End Sub
End Class